#include "Shot.h"
#include "../Plugin/PluginManager.h"
#include "../Scene/SceneNode.h"
#include "../GameEngine.h"
#include "../Audio/AudioManager.h"
#include "../Audio/Sound.h"

Plugin * Shot::usedPlugin = NULL;

Shot::Shot(int maxFrequency, std::string name) : GameEntity(maxFrequency, name)
{
	AudioManager::Instance()->getSound("shot.ogg")->play();
	COLOR tempcolor = {1,0,0};
	this->setColor(tempcolor);
	usedPlugin = PluginManager::Instance()->getPlugins(this->getName());

	/*POINT3D position = {10.5,10.0,5.0};
	setPosition(position);

	//Similar stuff to the orientation like translation getOrientation set Orientation
	ORIENTATION orientation = {0.5f,1.0,1.0,1.0};
	setOrientation(orientation);*/

	_colorState = COLORRED;
	if(name.compare("shipshot") == 0)
		_directionState = UP;
	else _directionState = DOWN;

	_shotPosition.x = NULL;
	_shotPosition.y = NULL;
	_shotPosition.z = NULL;
}

Shot::~Shot()
{
}

std::string Shot::getPluginName()
{
	return "NewUpdatePlugin";
}

void Shot::getNewPosition(POINT3D& pos)
{
}

void Shot::draw()
{
	POINT3D Top = {0.5, 1.0,0.0};
	POINT3D bottom = {0.5, 0.0, 0.0};
	POINT3D shotPos = _shotPosition;
	if(this->getName().compare("shipshot") == 0) {
		shotPos.x += 1.3f;
		shotPos.y += 2.0f;
	}
	GameEngine::Instance()->getRenderer()->translate(shotPos);
	GameEngine::Instance()->getRenderer()->rotate(getOrientation());
	//GameEngine::Instance()->getRenderer()->drawTriangle(Top,bottomLeft,bottomRight,getColor());
	GameEngine::Instance()->getRenderer()->drawLine(Top,bottom,getColor());
}
void Shot::actualUpdate(unsigned long long timePassed)
{

	if(_shotPosition.x == NULL && _shotPosition.y == NULL && _shotPosition.z == NULL) {
		_shotPosition.x = getPosition().x;
		_shotPosition.y = getPosition().y;
		_shotPosition.z = getPosition().z;
	}
	
	POINT3D newPosition = {_shotPosition.x, _shotPosition.y, _shotPosition.z};

	switch(_directionState) {

	case LEFT: 
		newPosition.x--;
		break;
	case RIGHT: 
		newPosition.x++;
		break;
	case UP: 
		newPosition.y++;
		break;
	case DOWN: 
		newPosition.y--;
		break;
	}
	_shotPosition = newPosition;
	
	MyGame::Instance()->getEnv()->collideWithShot(this,this->getShotPosition());
	if(this->getName().compare("shipshot") == 0) {
		if(newPosition.y > 13.0f)
			this->getMySceneNode()->destroy();
	}
	else {
		if(newPosition.y < -13.0f) {
			this->getMySceneNode()->destroy();
		}
	}

	switch(_colorState)
	{
		case COLORRED:	{COLOR tempColor = {1,0,0};
						setColor(tempColor);}
						break;
		case COLORBLUE:	{COLOR tempColor = {0,0,1};
						setColor(tempColor);}
						break;
	}
	//get and set the position from SceneNode
}

void Shot::reset(){}

int Shot::getType() {
	return SHIPSHOT;
}

POINT3D Shot::getShotPosition() {
	return _shotPosition;
}